/*
 * Copyright (c) 2008-2021 浩瀚深度 All Rights Reserved.
 *
 * <p>FileName: InitSyslog.java</p>
 *
 * @author zz
 * @date 2024/2/4
 * @version 1.0
 * History:
 * v1.0.0 2024/2/4 Create
 */
package com.monitor.config.init;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.monitor.config.SyslogConfig;
import com.monitor.entity.Syslog;
import com.monitor.mapper.SyslogMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * <p>Title: InitSyslog</p>
 * <p>Description: </p>
 *
 * @author zz
 */
@Component
@Slf4j
public class InitSyslog implements CommandLineRunner {

    @Autowired
    SyslogConfig syslogConfig;

    @Autowired
    private SyslogMapper syslogMapper;

    @Override
    public void run(String... args) throws Exception {
        log.info("预置SysLog初始化");
        try {
            if (!CollectionUtil.isNotEmpty(syslogMapper.selectAll())) {
                List<SyslogConfig.SyslogInfo> allConfigs = syslogConfig.getSyslog();
                if (!CollectionUtil.isEmpty(allConfigs)) {
                    allConfigs.forEach(syslogInfo -> {
                        Syslog syslog = BeanUtil.copyProperties(syslogInfo, Syslog.class);
                        syslogMapper.insert(syslog);
                    });
                }
            }
        } catch (Exception e) {
            log.error("初始化失败:" + e);
        }
    }
}
